<?php
namespace app\api\model;


use think\Model;
use app\api\model\traits\GoodsTrait;

class Cart extends Common
{
	use GoodsTrait;
	public function changeCart($cartId,$goodsId,$goodsCount,$goodsAttrIds='')
	{
		$cart = $this->find($cartId);
		return $this->createOrUpdateCart($cart,$cart->user_id,$goodsId,$goodsCount,$goodsAttrIds);
	}
	public function addCart($userId,$goodsId,$goodsCount,$goodsAttrIds='')
	{
		$cart = $this->where('user_id',$userId)->where('goods_id',$goodsId)->where('goods_attr_ids',$goodsAttrIds)->find();
		return $this->createOrUpdateCart($cart,$userId,$goodsId,$goodsCount,$goodsAttrIds);
	}
	
	public function createOrUpdateCart(?Cart $cart ,$userId,$goodsId,$goodsCount,$goodsAttrIds=''){
		if(empty($cart)){
			$cart = new Cart;
		}
		$cart->user_id = $userId;
		$cart->goods_id = $goodsId;
		$cart->goods_count = $goodsCount;
		$cart->goods_attr_ids = $goodsAttrIds;
		$cart->save();
		return $cart;
	}
}